package org.bird.lang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.URL;

/**
 * @description 自定义序列化
 * @author liangjf
 *
 */
public class Externalizable implements java.io.Externalizable {

	private transient String content = "是的，我将会被序列化，不管我是否被transient关键字修饰";

	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeObject(content);
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		content = (String) in.readObject();
	}

	public static void main(String[] args) throws Exception {
		URL url = Externalizable.class.getClassLoader().getResource("");
		Externalizable et = new Externalizable();
		ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(url.getPath() + "Externalizable.txt")));
		out.writeObject(et);

		ObjectInput in = new ObjectInputStream(new FileInputStream(new File(url.getPath() + "Externalizable.txt")));
		et = (Externalizable) in.readObject();
		System.out.println(et.content);

		out.close();
		in.close();
	}
}
